package com.community.backend.service.impl;

import com.alibaba.fastjson.JSON;
import com.community.backend.mapper.PropertyFeeMapper;
import com.community.backend.pojo.PropertyFee;
import com.community.backend.pojo.vo.PropertyFeeQueryVO;
import com.community.backend.pojo.dto.PropertyFeeQueryDTO;
import com.community.backend.service.PropertyFeeService;
import com.community.backend.utils.PageResult;
import com.community.backend.utils.RedisUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class PropertyFeeServiceImpl implements PropertyFeeService {
    private final PropertyFeeMapper propertyFeeMapper;

    public PropertyFeeServiceImpl(PropertyFeeMapper propertyFeeMapper) {
        this.propertyFeeMapper = propertyFeeMapper;
    }


    @Override
    public PageResult pageQuery(PropertyFeeQueryDTO propertyFeeQueryDTO) {
        PageHelper.startPage(propertyFeeQueryDTO.getCurrentPage(),propertyFeeQueryDTO.getPageSize());
        Page<PropertyFeeQueryVO> page = propertyFeeMapper.pageQuery(propertyFeeQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void save(PropertyFee propertyFee) {
        propertyFee.setId(UUID.randomUUID().toString());
        propertyFee.setStatus("未缴");
        propertyFee.setIsDelete("未删除");
        propertyFeeMapper.insert(propertyFee);
    }

    @Override
    public void update(PropertyFee propertyFee) {
        propertyFeeMapper.update(propertyFee);
    }

    @Override
    public void deleteById(String id) {
        PropertyFee propertyFee = new PropertyFee();
        propertyFee.setId(id);
        propertyFeeMapper.updateById(propertyFee);
    }

    @Override
    public PropertyFee propertyFeeDetail(String id) {
        PropertyFee propertyFee =propertyFeeMapper.selectById(id);
        return propertyFee;
    }

    @Override
    public void selectById(String id) {
        PropertyFee propertyFee = propertyFeeMapper.selectById(id);
        String userId = String.valueOf(propertyFee.getUserId());
        System.out.println("propertyFee"+propertyFee);
        Map<String,Object> map = new HashMap<>();
        map.put("sid",userId);
        map.put("message", "费用类型：" + propertyFee.getProject()); // 桌面通知 body
        map.put("feeId", propertyFee.getId());            // 扩展字段，前端可跳转详情
        map.put("status", propertyFee.getStatus());       // 已缴费/未缴费
        map.put("amount", propertyFee.getTotalPrice());   // 金额
        String json = JSON.toJSONString(Map.of(
                "sid", userId,
                "message", "您有新的" + propertyFee.getProject() + "账单,账单号："+propertyFee.getId() +"金额：" + propertyFee.getTotalPrice() + "元",
                "feeId", propertyFee.getId()));
        String key = "offline:" + userId;          // 不再拼 feeId
        System.out.println("key:"+key);
        System.out.println("json:"+json);
        RedisUtils.redis().opsForList().rightPush(key, json);
    }
}
